import QtQuick 2.0
import FileIO 1.0

QtObject {
    property string iniFilePath: ""

    property string version: ""

    property string config: ""

    function load() {
        config = FileIO.readTextFile(iniFilePath)
        if (!config) {
            version = ""
            return
        }

        let groups = config.match(/game_version=([^\s]+)/)
        version = (groups && groups[1]) || ""
    }

    function save() {
        if (!version) {
            console.error("Version for save is not set")
            return
        }

        if (!config) {
            config = `[General]\r\nchannel=1\r\ncps=mihoyo\r\ngame_version=${version}\r\nsub_channel=0\r\n`
        } else {
            config = config.replace(/(game_version)=[^\s]+/, "$1=" + version)
        }

        FileIO.writeTextFile(iniFilePath, config)
    }
}
